import { Injectable } from '@nestjs/common';
import { CreateUploadDto } from './dto/create-upload.dto';
import { UpdateUploadDto } from './dto/update-upload.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Upload } from './entities/upload.entity';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { join } from 'path';
import { createWriteStream, mkdir, mkdirSync, mkdtemp, readdirSync } from 'fs';
interface IUploaddBody {
  name: string;
  notename: string;
  mybase: string;
}
@Injectable()
export class UploadService {
  constructor(
    @InjectRepository(Upload)
    private upload: Repository<Upload>,
  ) {}
  create(createUploadDto: CreateUploadDto) {
    return 'This action adds a new upload';
  }

  async saveImg(body: IUploaddBody) {
    const data = await this.upload.create({
      name: body.name,
      mybase: body.mybase,
      notename: body.notename,
    });
    const result = await this.upload.insert(data);
    console.log('data', data);
    console.log('result', result);
    return 'good';
  }

  async getImg(name: string, notename: string) {
    console.log(name);
    const data = await this.upload.find({
      // select: ['mybase'],
      where: { name: name, notename: notename },
    });
    return data;
  }

  findOne(id: number) {
    return `This action returns a #${id} upload`;
  }

  update(id: number, updateUploadDto: UpdateUploadDto) {
    return `This action updates a #${id} upload`;
  }

  remove(id: number) {
    return `This action removes a #${id} upload`;
  }
  //判断当前磁盘是否有缓存
  async getdiskStorage(path) {
    try {
      mkdirSync(path);
      // MulterModule.register({
      //   storage: diskStorage({
      //     destination: path,
      //   }),
      // });
      return {
        message: '不存在',
      };
    } catch (error) {
      const files = readdirSync(path);
      // console.log(files);
      return {
        message: '存在',
        len: files,
      };
    }
  }
}
